/*
 * @(#) $Header$
 *
 * Copyright (C) 2007  Forklabs Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.media.jai.opimage;

import java.awt.RenderingHints;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.util.Collection;
import javax.media.jai.CollectionImage;
import javax.media.jai.CollectionImageFactory;
import javax.media.jai.CollectionOp;
import ca.forklabs.media.jai.CollectionImageUtil;
import ca.forklabs.media.jai.FormatDataType;
import ca.forklabs.media.jai.ParameterBlockUtil;
import ca.forklabs.media.jai.SpectralFilter3D;
import ca.forklabs.media.jai.operator.PipelineDescriptor;
import ca.forklabs.media.jai.operator.SpectralFilterDescriptor;
import ca.forklabs.media.jai.operator.SpectralHomomorphicDescriptor;

/**
 * Class {@code SpectralHomomorphicCIF} is a {@link CollectionImageFactory}
 * supporting the <em>spectralhomomorphic</em> operation.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.media.jai.opimage.SpectralHomomorphicCIF">Daniel Léonard</a>
 * @version $Revision$
 */
public class SpectralHomomorphicCIF implements CollectionImageFactory {

//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    */
   public SpectralHomomorphicCIF() {
   // nothing
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets the spectral filter.
    * @param   pb   the parameter block.
    * @return   the spectral filter.
    */
   protected SpectralFilter3D getFilter(ParameterBlock pb) {
      int index = SpectralHomomorphicDescriptor.FILTER_PARAMETER_INDEX;
      SpectralFilter3D filter = (SpectralFilter3D) pb.getObjectParameter(index);
      return filter;
      }

   /**
    * Gets the type for the format operation.
    * @param   pb   the parameter block.
    * @return   type for the format operation.
    */
   protected FormatDataType getDataType(ParameterBlock pb) {
      int index = SpectralHomomorphicDescriptor.TYPE_PARAMETER_INDEX;
      FormatDataType type = (FormatDataType) pb.getObjectParameter(index);
      return type;
      }


//---------------------------
// Implemented methods from javax.media.jai.CollectionImageFactory
//---------------------------

   /**
    * Creates a {@code CollectionImage} that represents the result of applying
    * the requested operation once to each image in the collection.
    * @param   pb   the parameter block.
    * @param   hints   the rendering hints.
    * @return   the collection image containing the results.
    */
   @SuppressWarnings({ "unchecked", "nls" })
   public CollectionImage create(ParameterBlock pb, RenderingHints hints) {
   // the real type is CollectionImage,
   // it is OK to see it as a collection
   // of rendered images, which it is.
      Collection<RenderedImage> sources = (Collection<RenderedImage>) pb.getSource(0);

      SpectralFilter3D filter = this.getFilter(pb);
      FormatDataType type = this.getDataType(pb);

      String[] operations = new String[] {
         "applytocollection",
         "applytocollection",
         "applytocollection",
         SpectralFilterDescriptor.NAME,
         "applytocollection",
         "applytocollection",
         };
      ParameterBlock[] pbs = new ParameterBlock[] {
         new ParameterBlock().add("format").add(ParameterBlockUtil.createFormatParameterBlock(null, type)),
         new ParameterBlock().add("addconst").add(ParameterBlockUtil.createAddConstParameterBlock(null, new double[] { 1.0, })),
         new ParameterBlock().add("log").add(ParameterBlockUtil.createLogParameterBlock(null)),
         new ParameterBlock().add(filter).add(type),
         new ParameterBlock().add("exp").add(ParameterBlockUtil.createExpParameterBlock(null)),
         new ParameterBlock().add("subtractconst").add(ParameterBlockUtil.createSubtractConstParameterBlock(null, new double[] { 1.0, })),
         };
      Collection<RenderedImage> collection = PipelineDescriptor.createCollection(sources, operations, pbs, hints);
      CollectionImage sinks = CollectionImageUtil.asCollectionImage(collection);
      return sinks;
      }

   /**
    * It is impratical to perform the update.
    * @param old_pb   ignored
    * @param old_hints   ignored
    * @param new_pb   ignored
    * @param new_hints   ignored
    * @param old_image   ignored
    * @param op   ignored
    * @return always {@code null}.
    */
   public CollectionImage update(ParameterBlock old_pb, RenderingHints old_hints,
                                 ParameterBlock new_pb, RenderingHints new_hints,
                                 CollectionImage old_image,
                                 CollectionOp op) {
   // it is impracticable to perform the update
      CollectionImage new_image = null;
      return new_image;
      }

   }
